{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def wordPattern(self, pattern, str):\n",
    "        \"\"\"\n",
    "        :type pattern: str\n",
    "        :type str: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        pattern = list(pattern)\n",
    "        str_list = str.split()\n",
    "        \n",
    "        if len(pattern) != len(str_list):\n",
    "            return False\n",
    "        \n",
    "        pair = {}\n",
    "        for i in range(len(pattern)):\n",
    "            # Never been recorded\n",
    "            if pattern[i] not in pair:\n",
    "                if str_list[i] not in pair.values():\n",
    "                    pair[pattern[i]] = str_list[i]\n",
    "                # Same word, different letter\n",
    "                else:\n",
    "                    return False\n",
    "            # Recorded, but mismatch\n",
    "            elif pair[pattern[i]] != str_list[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = Solution()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.wordPattern(\"abbc\", \"dog cat cat dag\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
